/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.cards;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.view.base.CardViewWrapper;
import it.gmariotti.cardslib.library.view.component.CardThumbnailView;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;

import java.io.IOException;

/**
 * This class provides a simple example of May Know Card
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class MayKnowCard extends Card {

    public MayKnowCard(Context context) {
        this(context, ResourceTable.Layout_carddemo_mayknow_inner_content);
    }

    public MayKnowCard(Context context, int innerLayout) {
        super(context, innerLayout);
        init();
    }

    private void init() {
        //Add Header
        CardHeader header = new MayKnowCardHeader(getContext(), ResourceTable.Layout_carddemo_mayknow_inner_header);
        try {
            header.setTitle(getContext().getResourceManager().getElement(ResourceTable.String_may_know_card_title).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        addCardHeader(header);
        setShadow(false);

        //Add Thumbnail
        CardThumbnail thumbnail = new CardThumbnail(getContext());
        thumbnail.setUrlResource("https://lh5.googleusercontent.com/-squZd7FxR8Q/UyN5UrsfkqI/AAAAAAAAbAo/VoDHSYAhC_E/s72/new%2520profile%2520%25282%2529.jpg");
        thumbnail.setErrorResource(ResourceTable.Media_ic_error_loadingsmall);
        addCardThumbnail(thumbnail);

        OnCardClickListener clickListener = (card, view) -> {
            //Do something
        };

        addPartialOnClickListener(Card.CLICK_LISTENER_CONTENT_VIEW, clickListener);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {

        try {
            Text title = (Text) view.findComponentById(ResourceTable.Id_carddemo_mayknow_main_inner_title);
            Text subtitle = (Text) view.findComponentById(ResourceTable.Id_carddemo_mayknow_main_inner_subtitle);
            Text add = (Text) view.findComponentById(ResourceTable.Id_carddemo_mayknow_main_inner_button);

            title.setText(getContext().getResourceManager().getElement(ResourceTable.String_may_know_card_inner_title).getString());
            subtitle.setText(getContext().getResourceManager().getElement(ResourceTable.String_may_know_card_inner_subtitle).getString());
            add.setClickable(true);

            CardViewWrapper cardView = getCardView();
            CardThumbnailView thumb = cardView.getInternalThumbnailLayout();
            if (thumb != null) {
                ComponentContainer.LayoutConfig lp = thumb.getLayoutConfig();
                lp.setMargins(25, 0, 0, 5);
            }
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }


    }

    class MayKnowCardHeader extends CardHeader {

        public MayKnowCardHeader(Context context, int innerLayout) {
            super(context, innerLayout);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {
            super.setupInnerViewElements(parent, view);

            try {
                Text t1 = (Text) view.findComponentById(ResourceTable.Id_carddemo_mayknow_main_inner_subtitle);
                if (t1 != null)
                    t1.setText(getContext().getResourceManager().getElement(ResourceTable.String_may_know_card_subtitle).getString());
            } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        }
    }
}
